<?php
class user_class
{
	function user_class()
	{
		global $sql;
		
		/*
		if (empty($_SESSION['user']))
		{
			if ($_COOKIE['pokerdb']<>"")
			{
				$id = (int)$_COOKIE['pokerdb'];
				if ($id<>0)
				{
					$sql->mysql_select("user", "*","`user_id` = ".$id);
					$row = $sql->get_line();
					if ( (md5($row['user_name'])==$_COOKIE['pokerdb1']) && (md5($row['user_password'])==$_COOKIE['pokerdb2']))
					{
						$_SESSION['user'] = $row;
					}
				}
			}
		}
		*/
		
		if (!is_array($_SESSION['user']['user_perm']))
		{
			if ($_SESSION['user']['user_admin']==1 && $_SESSION['user']['user_perm']<>1)
			{
				$_SESSION['user']['user_perm'] = explode(".", $_SESSION['user']['user_perm']);
			}
			elseif ($_SESSION['user']['user_perm']==1)
			{
				$_SESSION['user']['user_perm'] = array("all");
			}
		}
	}
	
	function get_user_registration()
	{
		return isset($_SESSION['user']['user_id']);
	}
	
	function get_user_admin()
	{
		return ($_SESSION['user']['user_admin']==1?TRUE:FALSE);
	}
	
	function get_user_perm($perm)
	{
		if ($this->get_user_admin())
		{
			if (in_array("all", $this->get_user_data("user_perm")) || in_array($perm, $this->get_user_data("user_perm")))
			{
				return TRUE;
			}
		}
		
		$_SESSION['message']['error'][] = "Nincs jogod a lap megtekintéséhez!";
		header("Location: index.php");
		die();
	}
	
	function get_user_data($type='user_id')
	{
		return $_SESSION['user'][$type];
	}
	
	function get_avatar($avatar,$intag=true)
	{	
		if ($avatar=="")
		{
			$avatar = "-upload-avatar.jpg";
		}
		if (stristr($avatar, "-upload-") !== FALSE)
		{
			$avatar = str_replace("-upload-", "image/avatar/", $avatar);
		}
		
		if ($intag)
		{
			return "<img src='".$avatar."' class='user_avatar'>";
		}
		else
		{
			return $avatar;
		}
	}
	
	function user_login($username, $userpassword)
	{
		global $sql;
		
		$username = $sql->escape($username);
		$userpassword = md5($userpassword);
		
		$sql->mysql_select("users", "*","`user_login` = '".$username."' and `user_password` = '".$userpassword."' and `user_admin` = 1 and `user_ban` = 0");
		
		if ($sql->get_mysql_num_row() == 1)
		{
			$_SESSION['user'] = $sql->get_line();
			
			//$sql->mysql_update("users", array("user_visits" => "user_visits + 1"),"`user_id` = ".$_SESSION['user']['user_id'],"",false);
			if ($_SESSION['user']['user_admin']==1 && $_SESSION['user']['user_perm']<>1)
			{
				$_SESSION['user']['user_perm'] = explode(".", $_SESSION['user']['user_perm']);
			}
			elseif ($_SESSION['user']['user_perm']==1)
			{
				$_SESSION['user']['user_perm'] = array("all");
			}
			
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}

?>